home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2355 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.5 KB  |  74 lines

  1. Path: news.bridge.net!news
  2. From: David Byrden <100101.2547@compuserve.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Creating a pointer to a function "void (*ptrFunction)()"  inside a class
  5. Date: 17 Jan 1996 08:14:52 GMT
  6. Organization: self-employed
  7. Message-ID: <4dib5s$92t@news.bridge.net>
  8. References: <30ECA10F.3D99@ifu.net> <4crkle$h4r@gold.datalytics.com>
  9. NNTP-Posting-Host: ppp-mia1-38.bridge.net
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.1N (Windows; I; 16bit)
  14.  
  15.  
  16. Jason;
  17.  
  18. >       I am trying to create a base class that has a function that the
  19. >derived class needs to create.  In order to do this, I want to create a
  20. >pointer to a function in the base class that the derived class can then
  21. >define.  This is not a case where an override will work because the
  22. >number and names of the derived functions is not known.
  23.  
  24.  
  25. Jason, quite apart from the issue of the object address, you simply 
  26. cannot assign the address of a function taking two ints, to a pointer to 
  27. function taking void, as your code attempted:
  28.  
  29. >>   int myFunction(int, int);
  30. >>   int (*ptrFunction)();
  31. >>   ptrFunction = myFunction;
  32.  
  33. If your compiler allows this, it is seriously deficient. C++ 
  34. distinguishes all functions, member or nonmember, by their parameter type 
  35. list as well as everything else. The name-mangling, contrary to what 
  36. Robert said, is applied to ALL function names to encode this information.
  37.  
  38. Member functions do indeed have a hidden parameter, the object's 
  39. address.You can declare a pointer to base class member function in 
  40. your class like so;
  41.  
  42.  
  43.  class BaseClass
  44.  {
  45.         int (BaseClass::*ptrFunction)();
  46.  }
  47.  
  48.  
  49. You can call it, to operate on an object, like so;
  50.  
  51.  
  52.     (myObject .* ptrFunction) ( parameters )
  53.  
  54. But, since your one is itself a **member of a class object**, you will 
  55. call it like so:
  56.  
  57.     (myObject .* (yourObject.ptrFunction) ) ( parameters )
  58.  
  59. It is interesting that the object in which the pointer resides, can be 
  60. different from the object on which you call the function, as I show here. 
  61. You probably don't need this flexibility so you will probably write a 
  62. member function to encapsulate all this nasty stuff.
  63.  
  64.  
  65. You can indeed call through the pointer in a derived class object, and 
  66. **as far as I can remember** the derived class function will work 
  67. correctly as long as the object is indeed of the correct derived class 
  68. for it. That makes sense to me, but don't take my word for it, I should 
  69. really go and look it up. But it's 3 am.  :)
  70.  
  71.          David
  72.  
  73.  
  74.